package com.sunyur.tailor.channel.service.inner.address.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;
import com.sunyur.tailor.channel.common.inner.util.SunyurClientUtil;
import com.sunyur.tailor.channel.common.inner.util.SunyurResultUtil;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.address.model.AddressListModel;
import com.sunyur.tailor.channel.service.inner.address.param.AddressListParam;
import com.sunyur.tailor.channel.service.inner.address.service.SunyurAddressService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description 地址相关接口实现
 * @Author XuHui
 * @Date 2021/12/30 14:35
 * @Version 1.0
 */
@Service("sunyurAddressService")
public class SunyurAddressServiceImpl implements SunyurAddressService {

    /**
     * 请求商越Client
     */
    @Autowired
    private SunyurClientUtil sunyurClient;

    @Value("${sunyur.api.getAddressList}")
    private String getAddressList;

    @Override
    public ChannelResult<List<AddressListModel>> getAddressList(AddressListParam addressListParam) throws Exception {
        if (ObjectUtils.isEmpty(addressListParam)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), "SunyurAddressServiceImpl getAddressList param is null");
        }
        // 调用主线接口
        SunyurResult<List<AddressListModel>> sunyurResult = sunyurClient.execPost(getAddressList, JSON.toJSONString(addressListParam), new TypeReference<SunyurResult<List<AddressListModel>>>() {
        });
        // 主线报错
        if (!SunyurResultUtil.isSuccess(sunyurResult)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
        }
        return ChannelResultUtil.success(sunyurResult.getContent());
    }
}
